/*
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2012 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package au.gov.aims.metadatagenerator;

import au.gov.aims.metadatagenerator.entity.Project;
import au.gov.aims.metadatagenerator.exporter.Exporter;
import au.gov.aims.metadatagenerator.exporter.NERPProjectExporter;
import au.gov.aims.metadatagenerator.importer.Importer;
import au.gov.aims.metadatagenerator.importer.NERPProjectImporter;

import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Generator {
	private static final Logger LOGGER = Logger.getLogger(Generator.class.getName());

	private static final String SYNOPSIS =
			"java -jar metadata-generator-0.1-jar-with-dependencies.jar [CSVFile] [exportAllProjects]\n" +
				"\tCSVFile: Coma separated values file that contains the metadata of the projects to migrate\n" +
				"\texportAllProjects [true|false]: True to export every projects. False to export\n" +
					"\t\tonly the projects that has the word TRUE in the first line of their project.\n" +
					"\t\tDefault: false.\n" +
			"Example: java -jar metadata-generator-0.1-jar-with-dependencies.jar /path/to/projects-file.csv true\n" +
			"\n" +
			"Upload the XML documents to the server and load them in GeoNetwork using the Admin interface:\n" +
				"\tAdministration -> Import, export & harvesting -> Batch Import";

	public static void main(String[] args) throws Exception {
		if (args.length < 1 || args.length > 2) {
			System.err.println("ERROR: Illegal arguments.");
			System.err.println(SYNOPSIS);
			return;
		}

		String filePath = args[0]; // "/home/glafond/Desktop/projects/nerp-project_metadata-importer/project-metadata-final_metadata-tab.csv"
		boolean exportAllProjects = false;
		if (args.length >= 2) {
			exportAllProjects = Boolean.parseBoolean(args[1]);
		}

		File projectsFile = new File(filePath);
		File folder = projectsFile.getParentFile();
		Importer<Project> importer = new NERPProjectImporter();
		Exporter<Project> exporter = new NERPProjectExporter();

		List<Project> projects = importer.importFile(projectsFile);

		File outputFolder = new File(folder, "output");
		outputFolder.mkdirs();
		File outputFile;
		for (Project project : projects) {
			if (exportAllProjects || project.isCompleted()) {
				String filename = "NERP TE Project " + project.getProjectNumber() + " " + project.getUUID();
				outputFile = new File(outputFolder, filename.replaceAll("[^a-zA-Z0-9_\\-]", "_") + ".xml");
				exporter.exportToFile(project, outputFile);

				LOGGER.log(Level.INFO, "Saved project\n\tNAME: {0}\n\tFile: {1}", new String[]{project.getTitle(), outputFile.getAbsolutePath()});
			} else {
				LOGGER.log(Level.INFO, "Skipping incomplete project\n\tNAME: {0}", project.getTitle());
			}
		}
	}
}
